<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\model;
use think\facade\Db;
class LuckyCollage
{
	//幸运拼团开奖
	static function kaijiang($order){
		$aid = $order['aid'];
		$mid = $order['mid'];
		$orderlist = Db::name('lucky_collage_order')->where(['teamid'=>$order['teamid'],'status'=>1])->select()->toArray();
		//取出商品设置
		$product = Db::name('lucky_collage_product')->where(['id'=>$order['proid']])->find();
		$teamarr1 = array_column($orderlist, 'mid');

		$bzjarr = [];
		$zjarr = [];
		$zjcount = 0;
		$teamarr1 = array_diff($teamarr1, $zjarr);	//去掉中奖的id
		$teamarr = array_diff($teamarr1, $bzjarr);  //去掉不中奖的id
		$syzjnum = intval($product['gua_num']-$zjcount);
		$selected = array_rand($teamarr,$syzjnum);
		if($syzjnum>0 && $syzjnum==1){
			$newteam[] = $teamarr[$selected];	//中奖的
			unset($teamarr[$selected]);//不中奖
		}elseif($syzjnum>1){
			foreach($selected as $s){
				$newteam[] = $teamarr[$s]; //中奖的订单
				unset($teamarr[$s]);
			}
		}else{
			$newteam=[];
		}
		//中将的会员id = 设置好的中将id+其他随机出来的id
		$newteam = array_merge($zjarr,$newteam);
		//不中奖的会员id = 设置好的id+其他随机出来的id
		$teamarr = array_merge($bzjarr,$teamarr);

		if($newteam){
			$stock=0;
			//中奖的人数
			foreach($newteam as $v){
				$orderinfo = Db::name('lucky_collage_order')->where('mid',$v)->where('teamid',$order['teamid'])->where('status',1)->find();
				$guige = Db::name('lucky_collage_guige')->field('stock')->where('aid',$aid)->where('id',$orderinfo['ggid'])->find();
				Db::name('lucky_collage_guige')->where('aid',$aid)->where('id',$orderinfo['ggid'])->update(['stock'=>$guige['stock']-$orderinfo['num']]);
				$stock+=$orderinfo['num'];
				$members = Db::name('member')->where('id',$v)->find();
				if($orderinfo['isjiqiren']==1){
					$members = Db::name('lucky_collage_jiqilist')->where('id',$orderinfo['mid'])->find();
				}
				if(getcustom('plug_luckycollage') || getcustom('luckycollage_teamfenhong')){   //中奖者 奖励积分/余额/佣金/优惠券
					if($product['zjjl_type']==1 && $product['zj_money']>0){  //1 奖励余额
						\app\common\Member::addmoney($aid,$orderinfo['mid'],$product['zj_money'],'参加拼团中奖奖励'.t('余额'));
					}else if($product['zjjl_type']==2 && $product['zjscore'] > 0){  //2 奖励积分
						\app\common\Member::addscore($aid,$orderinfo['mid'],$product['zjscore'],'参加拼团中奖奖励'.t('积分'));
					}else if($product['zjjl_type']==3 && $product['zjcommission'] > 0){  //3 奖励佣金
						\app\common\Member::addcommission($aid,$orderinfo['mid'],$orderinfo['mid'],$product['zjcommission'],'参加拼团中间费奖励'.t('佣金'));
					}elseif($product['zjjl_type']==4 && $product['zj_yhqids']){ //奖励优惠券
						$yhqids = explode(',',$product['zj_yhqids']);
						foreach($yhqids as $yhqid){
							\app\common\Coupon::send($aid,$orderinfo['mid'],$yhqid);
						}
					}
				}else if($orderinfo['givescore'] > 0){
					\app\common\Member::addscore($aid,$orderinfo['mid'],$orderinfo['givescore'],'购买产品奖励'.t('积分'));
				}
				//自动发货
				if($orderinfo['freight_type']==3){
					$freight_content = Db::name('lucky_collage_product')->where('id',$orderinfo['proid'])->value('freightcontent');
					Db::name('lucky_collage_order')->where('id',$orderinfo['id'])->update(['freight_content'=>$freight_content,'status'=>2,'send_time'=>time()]);

                    //发货信息录入 微信小程序+微信支付
                    if($orderinfo['platform'] == 'wx' && $orderinfo['paytypeid'] == 2){
                        \app\common\Order::wxShipping($orderinfo['aid'],$orderinfo,'lucky_collage');
                    }
				}
				//在线卡密
				if($orderinfo['freight_type']==4){
					$codelist = Db::name('lucky_collage_codelist')->where('proid',$orderinfo['proid'])->where('status',0)->order('id')->limit($orderinfo['num'])->select()->toArray();
					if($codelist && count($codelist) >= $orderinfo['num']){
						$pscontent = [];
						foreach($codelist as $codeinfo){
							$pscontent[] = $codeinfo['content'];
							Db::name('lucky_collage_codelist')->where('id',$codeinfo['id'])->update(['orderid'=>$orderinfo['id'],'ordernum'=>$orderinfo['ordernum'],'headimg'=>$members['headimg'],'nickname'=>$members['nickname'],'buytime'=>time(),'status'=>1]);
						}
						$pscontent = implode("\r\n",$pscontent);
						Db::name('lucky_collage_order')->where('id',$orderinfo['id'])->update(['freight_content'=>$pscontent,'status'=>2,'send_time'=>time()]);
					}
                    //发货信息录入 微信小程序+微信支付
                    if($orderinfo['platform'] == 'wx' && $orderinfo['paytypeid'] == 2){
                        \app\common\Order::wxShipping($orderinfo['aid'],$orderinfo,'lucky_collage');
                    }
				}
				$team = Db::name('lucky_collage_order_team')->where('aid',$aid)->where('id',$order['teamid'])->find();
				if($orderinfo['isjiqiren']!=1){
					//公众号通知 拼团成功通知
					$tmplcontent = [];
					$tmplcontent['first'] = '有新拼团订单拼团成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $order['title']; //商品名称
					$tmplcontent['keyword2'] = $members['nickname'];//团长
					$tmplcontent['keyword3'] = $team['teamnum'];//成团人数
					$tmplcontent['first'] = '恭喜您拼团成功';
					$rs = \app\common\Wechat::sendtmpl($aid,$orderinfo['mid'],'tmpl_collagesuccess',$tmplcontent,m_url('activity/luckycollage/orderlist', $aid));
					//订阅消息
					$tmplcontent = [];
					$tmplcontent['thing1'] = $order['title'];
					$tmplcontent['thing10'] = $members['nickname'];
					$tmplcontent['number12'] = $team['teamnum'];

					$tmplcontentnew = [];
					$tmplcontentnew['thing7'] = $order['title'];
					$tmplcontentnew['thing12'] = $members['nickname'];
					$tmplcontentnew['number2'] = $team['teamnum'];
					\app\common\Wechat::sendwxtmpl($aid,$orderinfo['mid'],'tmpl_collagesuccess',$tmplcontentnew,'activity/luckycollage/orderlist',$tmplcontent);
					//短信通知
					if($members['tel']){
						$rs = \app\common\Sms::send($aid,$members['tel']?$members['tel']:$orderinfo['tel'],'tmpl_collagesuccess',['ordernum'=>$orderinfo['ordernum']]);
					}
				}
				Db::name('lucky_collage_order')->where('id',$orderinfo['id'])->update(['iszj'=>1]);
				//增加销量
                \app\model\Payorder::addSales($orderinfo['id'],'lucky_collage',$aid,$orderinfo['bid'],$orderinfo['num']);
			}
			//减掉库存
			Db::name('lucky_collage_product')->where('aid',$aid)->where('id',$product['id'])->update(['stock'=>$product['stock']-$stock]);
		}
		//未中奖
		foreach($teamarr as $t){
			$orderinfo = Db::name('lucky_collage_order')->where('teamid',$order['teamid'])->where('status',1)->where('mid',$t)->find();
			//var_dump($orderinfo);
			if(!$orderinfo['isjiqiren']){
				//幸运拼团定制
				if(false){}else{
					//奖励红包  red_give_mode 1返到余额，2 是返到零钱 fy_type 1 按比例 fy_money，2 是按金额 fy_money_val
					if($product['fy_type']==1) $money = number_format($product['sell_price']*$product['fy_money']/100,2);
					if($product['fy_type']==2) $money = $product['fy_money_val'];
					if($product['red_give_mode']==1){
						\app\common\Member::addmoney($orderinfo['aid'],$orderinfo['mid'],$money,'拼团未中奖奖励');
					}else if($product['red_give_mode']==2){
						\app\common\Wxpay::transfers($orderinfo['aid'],$orderinfo['mid'],$money,'',$orderinfo['platform'],'拼团未中奖奖励');
					}
				}
				if($product['tklx']=='1'){
					$paytype = $orderinfo['paytypeid'];
					\app\common\Order::refund($orderinfo,$orderinfo['totalprice'],'拼团未中奖订单退款');
				}else{
					\app\common\Member::addmoney($orderinfo['aid'],$orderinfo['mid'],$orderinfo['totalprice'],'拼团未中奖退款');
				}
				//积分抵扣的返还
				if($orderinfo['scoredk'] > 0){
					\app\common\Member::addscore($orderinfo['aid'],$orderinfo['mid'],$orderinfo['scoredk'],'拼团未中奖退款返还');
				}
				//优惠券抵扣的返还
				if($orderinfo['coupon_rid'] > 0){
					Db::name('coupon_record')->where('aid',$aid)->where('mid',$orderinfo['mid'])->where('id',$orderinfo['coupon_rid'])->update(['status'=>0,'usetime'=>'']);
				}
				if($orderinfo['bid'] > 0){
					$binfo = Db::name('business')->where('aid',$aid)->where('id',$orderinfo['bid'])->find();
					if($binfo['money'] < $money){
						$money = 0;
					}else{
						\app\common\Business::addmoney($aid,$order['bid'],-$money,'给幸运拼团未中奖成员发放奖励：'.$order['ordernum']);
					}
				}

				//模板消息
				//退款成功通知
				$tmplcontent = [];
				if($product['tklx']=='1'){
					$tmplcontent['first'] = '您的订单已经完成退款，¥'.$orderinfo['totalprice'].'已经退回您的付款账户，请留意查收。';
				}else{
					$tmplcontent['first'] = '您的订单已经完成退款，¥'.$orderinfo['totalprice'].'已经退回您的余额账户，请留意查收。';
				}
				$tmplcontent['remark'] = '拼团成功，未中奖订单退款'.($money > 0 ? '并奖励红包'.$money.'元' : '').'，请点击查看详情~';
				$tmplcontent['orderProductPrice'] = $orderinfo['totalprice'];
				$tmplcontent['orderProductName'] = $orderinfo['title'];
				$tmplcontent['orderName'] = $orderinfo['ordernum'];
                $tmplcontentNew = [];
                $tmplcontentNew['character_string1'] = $orderinfo['ordernum'];//订单编号
                $tmplcontentNew['thing2'] = $orderinfo['title'];//商品名称
                $tmplcontentNew['amount3'] = $orderinfo['totalprice'];//退款金额
				\app\common\Wechat::sendtmpl($aid,$orderinfo['mid'],'tmpl_tuisuccess',$tmplcontent,m_url('activity/luckycollage/orderlist'),$tmplcontentNew);
				//订阅消息
				$tmplcontent = [];
				$tmplcontent['amount6'] = $orderinfo['totalprice'];
				$tmplcontent['thing3'] = $orderinfo['title'];
				$tmplcontent['character_string2'] = $orderinfo['ordernum'];

				$tmplcontentnew = [];
				$tmplcontentnew['amount3'] = $orderinfo['totalprice'];
				$tmplcontentnew['thing6'] = $orderinfo['title'];
				$tmplcontentnew['character_string4'] = $orderinfo['ordernum'];
				\app\common\Wechat::sendwxtmpl($aid,$orderinfo['mid'],'tmpl_tuisuccess',$tmplcontentnew,'activity/luckycollage/orderlist',$tmplcontent);

				//短信通知
				$member = Db::name('member')->where('id',$orderinfo['mid'])->find();
				if($member['tel']){
					$tel = $member['tel'];
				}else{
					$tel = $orderinfo['tel'];
				}
				$rs = \app\common\Sms::send($aid,$tel,'tmpl_tuisuccess',['ordernum'=>$orderinfo['ordernum'],'money'=>$orderinfo['totalprice']]);
			}
			Db::name('lucky_collage_order')->where('id',$orderinfo['id'])->update(['status'=>4,'refund_status'=>2,'refund_time'=>time(),'refund_money'=>$orderinfo['totalprice'],'refund_reason'=>'拼团未中奖退款','iszj'=>2]);

		}
		}
	static function membertag_collage($mid,$teamid,$product){
        }
}